import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class App {
    public static char[] charArray=new char[52];
    public static void main(String[] args) {
        initCharArray();
        Scanner sc=new Scanner(System.in);
        ArrayList<User> list=new ArrayList<>();
        boolean flag=false;
        while (true){
            System.out.println("欢迎来到学生管理系统");
            System.out.println("请选择操作1登录 2注册 3忘记密码 4退出");
            //0退出,1注册,2登录,3忘记密码
            String choose=sc.next();
            switch(choose){
                case "1"-> login(list);
                case "2"-> register(list);
                case "3"-> forgetPassword(list);
                case "4"->{
                    flag=true;
                    System.out.println("退出");
                }
                default -> System.out.println("无该选项");
            }
            if(flag){
                break;
            }
        }
    }
    public static void login(ArrayList<User> list){
        Scanner sc=new Scanner(System.in);
        System.out.println("登录");
        for (int i = 0; i < 3; i++) {
            System.out.println("请输入用户名");
            String userName=sc.next();
            System.out.println("请输入密码");
            String password=sc.next();
            String VerificationCode=getRandomCode();
            System.out.println("验证码是:"+VerificationCode);
            System.out.println("请输出验证码");
            String code=sc.next();
            if(code.equalsIgnoreCase(VerificationCode)==false){
                System.out.println("验证码有误");
                return;
            }
            User u= new User(userName,password,null,null);
            if(check(list,u,"login")==false){
                return;
            }
            int index=getindex (list,new User(userName,null,null,null));
            if(index==-1){
                System.out.println("该用户不存在,请先注册");
                return;
            }
            //用户名存在
            if(checkall(list,index,u,"login")){
                System.out.println("信息正确,正在进入学生管理系统");
                StudentSystem.main();
                break;
            }else{
                if(i==2){
                    System.out.println(userName+"已经被锁定,请联系官方客服进行解封");
                }else{
                    System.out.println("还剩下"+(2-i)+"次机会");
                }
            }
        }

    }
    public static boolean checkall(ArrayList<User> list,int index,User u,String way){
        if(u.getPassword().equals(list.get(index).getPassword())==false){
            System.out.println("密码错误");
            return false;
        }
        String wayans="login";
        if(way.equals(wayans)){
            return true;
        }
        //走到这里说明是忘记密码的情况,要验证身份号和手机号
        if(u.getPersonId().equals(list.get(index).getPersonId())==false){
            System.out.println("身份证号有误");
            return false;
        }
        if(u.getPhoneNumber().equals(list.get(index).getPhoneNumber())==false){
            System.out.println("手机号有误");
            return false;
        }
        return true;
    }
    public static void initCharArray(){
        for (int i = 0; i < 52; i++) {
            charArray[i]=(char)(i+'a');
            if(i>25){
                charArray[i]=(char)(i-26+'A');
            }
        }
    }
    public static String getRandomCode(){
        Random r=new Random();
        StringBuilder sb=new StringBuilder();
        for (int i = 0; i < 4; i++) {
            char c=charArray[r.nextInt(52)];
            sb.append(c);
        }
        sb.append((char)(r.nextInt(10)+'0'));
        int index=r.nextInt(5);
        char[] str=sb.toString().toCharArray();
        char temp=str[index];
        str[index]=str[str.length-1];
        str[str.length-1]=temp;
        String ans=new String(str);
        return ans;
    }
    public static void register(ArrayList<User> list){
        Scanner sc=new Scanner(System.in);
        System.out.println("注册");
        String userName,password,newpassword,personId,phoneNumber;
        //检查用户名
        while (true){
            System.out.println("请输入用户名");
            userName=sc.next();
            if(check(list,new User(userName,null,null,null),"register")){
                System.out.println("用户名正确,请输入其他信息:");
                break;
            }else{
                System.out.println("用户名格式有误,请重新输入");
            }
        }
        //检查密码
        while (true){
            System.out.println("请输入第一次密码");
            password=sc.next();
            System.out.println("请输入第二次密码");
            newpassword=sc.next();
            if(password.equals(newpassword)){
                System.out.println("密码正确,请输入其他信息:");
                break;
            }else{
                System.out.println("两次密码不一致,请重新输入");
            }
        }
        //检查身份证号
        while (true){
            System.out.println("请输入身份证号");
            personId=sc.next();
            if(check(list,new User(null,null,personId,null),"register")==false){
                System.out.println("身份证号格式有误请重新输入");
            }else{
                System.out.println("身份证号格式正确,请输入其他信息");
                break;
            }
        }
        //检查手机号
        while (true){
            System.out.println("请输入手机号");
            phoneNumber=sc.next();
            if(check(list,new User(null,null,null,phoneNumber),"register")==false){
                System.out.println("手机号格式有误请重新输入");
            }else{
                System.out.println("手机号格式正确");
                break;
            }
        }
        User u=new User(userName,password,personId,phoneNumber);
        list.add(u);
    }
    public static boolean check(ArrayList<User>list,User u,String way){
        String way0="login";
        String userName=u.getUserName();
        String personId=u.getPersonId();
        String phoneNumber=u.getPhoneNumber();
        if(userName!=null){
            if(checkUserName(list,userName)==false){
                return false;
            }
            if(way0.equalsIgnoreCase(way)==false && checkUserNameOnly(list,userName)==false){
                System.out.println("用户名不唯一");
                return false;
            }
        }
        if(personId!=null){
            if(checkPersonId(personId)==false){
                return false;
            }
        }
        if(phoneNumber!=null){
            if(checkPhoneNumber(phoneNumber)==false){
                return false;
            }
        }
        return true;
    }
    public static boolean checkUserNameOnly(ArrayList<User> list,String userName){
        int size=list.size();
        for (int i = 0; i < size; i++) {
            if(userName.equals(list.get(i).getUserName())){
                return false;
            }
        }
        return true;
    }
    public static int getindex(ArrayList<User> list,User u){
        int size=list.size();
        String userName=u.getUserName();
        for (int i = 0; i < size; i++) {
            if(list.get(i).getUserName().equals(userName)){
                return i;
            }
        }
        return -1;
    }
    public static boolean checkPersonId(String personId){
        int len=personId.length();
        if(len!=18){
            return false;
        }
        if(personId.charAt(0)=='0'){
            return false;
        }
        for (int i = 0; i < len - 1; i++) {
            char c=personId.charAt(i);
            if(c<'0'||c>'9'){
                return false;
            }
        }
        char c=personId.charAt(len-1);
        if(c=='X'||c=='x'||(c>='0'&&c<='9')){
            return true;
        }
        return false;
    }
    public static boolean checkPhoneNumber(String number){
        int len=number.length();
        if(len!=11){
            return false;
        }
        for (int i = 0; i < len; i++) {
            char c=number.charAt(i);
            if(c<'0'||c>'9'){
                return false;
            }
        }
        return true;
    }
    public static boolean checkUserName(ArrayList<User> list,String userName){
        //先检查格式再检查唯一,以为后面需要用到网络,数据库
        //数据库的数据是指定格式的
        //不能和非法数据判定唯一性
        //求len,避免多次调用,降低效率
        int len=userName.length();
        //1判断长度
        if(len<3||len>15){
            return false;
        }
        //2判断内容
        boolean alphabte=false,number=false;
        for (int i = 0; i < len; i++) {
            char c=userName.charAt(i);
            if((c>='a'&&c<='z' )||(c>='A'&&c<='Z')){
                alphabte=true;
            }
            if(c>='0'&&c<='9'){
                number=true;
            }
            if(alphabte && number){
                break;
            }
        }
        if(alphabte==false || number==false){
            return false;
        }
        return true;
    }
    public static void forgetPassword(ArrayList<User> list){
        Scanner sc=new Scanner(System.in);
        System.out.println("忘记密码");
        System.out.println("请输入用户名");
        String userName=sc.next();
        int index=getindex(list,new User(userName,null,null,null));
        if(index==-1){
            System.out.println("用户名不存在");
            return;
        }
        //用户名存在
        User u=list.get(index);
        String realPersonId=u.getPersonId();
        System.out.println("请输入身份证号:");
        String personId=sc.next();
        if(personId.equals(realPersonId)==false){
            System.out.println("身份证号错误");
            return;
        }
        String realPhoneNumber=u.getPhoneNumber();
        System.out.println("请输入手机号");
        String phoneNumber=sc.next();
        if(phoneNumber.equals(realPhoneNumber)==false){
            System.out.println("手机号错误");
            return;
        }
        while (true){
            System.out.println("请输入要修改后的密码");
            String code1=sc.next();
            System.out.println("请第二次输入要修改后的密码");
            String code2=sc.next();
            if(code1.equals(code2)==false){
                System.out.println("两次密码不一致,请重新输入");
            }else{
                u.setPassword(code1);
                System.out.println("密码修改成功");
                break;
            }
        }
    }
}
